package com.fs.util;

import com.benjaminwan.ocrlibrary.OcrResult;
import io.github.mymonstercat.Model;
import io.github.mymonstercat.ocr.InferenceEngine;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Wordrecognize {
    public static String wordrecognize(String imagePath) {
        // 定义图片文件的完整路径
        Path path = Paths.get("C:/uploads/images", imagePath);

        try {
            // 检查文件是否存在
            if (!Files.exists(path)) {
                throw new FileNotFoundException("File not found: " + imagePath);
            }

            // 初始化引擎并运行OCR
            InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
            OcrResult ocrResult = engine.runOcr(path.toString());

            // 返回识别结果
            return ocrResult.getStrRes().trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        // 传递图片文件名
        String result = wordrecognize("1717502076065.jpg");
        if (result != null) {
            System.out.println(result);
        }
    }
}